/**
 * 
 */
package cm.dpassyann.speedDating.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.DisplayMetrics;
import android.util.Log;

/**
 * @author YDEOUNGO
 * @description Utility Class
 */
public class SpeedatingUtil {
	
	private static SpeedatingUtil instance = new SpeedatingUtil();
	static Context _context;
	ConnectivityManager connectivityManager;
//	NetworkInfo wifiInfo, mobileInfo;

	/**
	 * Singleton Constructor
	 * @param context The Activity context
	 * @return return the Instance of SpeedDatingUtil
	 */
	public static SpeedatingUtil getInstance(Context context) {
		_context = context;
		return instance;
	}

	/**
	 * Checks if the device is connected
	 * @param context The activity context
	 * @return returns true if the device is connected, false otherwise
	 */
	public boolean isOnline(Context context) {
		try {
			connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

//			mobileInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
//			wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
			return networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected();

		} catch (Exception e) {
			System.out.println("CheckConnectivity Exception: " + e.getMessage());
			Log.v("connectivity", e.toString());
		}
		return false;
	}
	
	/**
	 * Checks if the device is a tablet or a phone
	 * @param context The Activity Context.
	 * @return Returns true if the device is a Tablet
	 */
	public boolean isTabletDevice(Context context) {
	    // Verifies if the Generalized Size of the device is XLARGE to be
	    // considered a Tablet
	    boolean xlarge = ((context.getResources().getConfiguration().screenLayout & 
	                        Configuration.SCREENLAYOUT_SIZE_MASK) == 
	                        Configuration.SCREENLAYOUT_SIZE_XLARGE);

	    // If XLarge, checks if the Generalized Density is at least MDPI
	    // (160dpi)
	    if (xlarge) {
	        DisplayMetrics metrics = new DisplayMetrics();
	        Activity activity = (Activity) context;
	        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

	        // MDPI=160, DEFAULT=160, DENSITY_HIGH=240, DENSITY_MEDIUM=160,
	        // DENSITY_TV=213, DENSITY_XHIGH=320
	        if (metrics.densityDpi == DisplayMetrics.DENSITY_DEFAULT
	                || metrics.densityDpi == DisplayMetrics.DENSITY_HIGH
	                || metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM
	                || metrics.densityDpi == DisplayMetrics.DENSITY_TV
	                || metrics.densityDpi == DisplayMetrics.DENSITY_XHIGH) {

	            // Yes, this is a tablet!
	            return true;
	        }
	    }

	    // No, this is not a tablet!
	    return false;
	}
}
